package com.tql.pandian.controller;

import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.model.LoginUser;
import com.tql.pandian.entity.ResultDTO;
import com.tql.pandian.entity.TqlPandianPeriodEntity;
import com.tql.pandian.entity.TqlPandianPeriodShopEntity;
import com.tql.pandian.entity.dto.PanDianPeriodDTO;
import com.tql.pandian.entity.dto.PeriodPcListDTO;
import com.tql.pandian.entity.vo.PandianVo;
import com.tql.pandian.service.IPandianPeriodService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 *     //盘点时间段 tql_pandian_period
 *     //盘点时间段与店铺关系表 tql_pandian_period_shop
 *     //店铺开通的时候用
 *     shopNumber
 */
@Api("盘点时间设置")
@RestController
@RequestMapping("/pandianPeriod")
public class PandianPeriodController extends BaseController {

    @Autowired
    private IPandianPeriodService pandianPeriodService;

   // @RequiresPermissions("pandian:pandianPeriod:list")
   @ApiOperation("盘点列表")
    @GetMapping("/list")
    public AjaxResult list(String shopName)
    {
        LoginUser loginUser= SecurityUtils.getLoginUser();
        Long userId=loginUser.getUserid();
        boolean flag= SecurityUtils.isAdmin(userId);
        TqlPandianPeriodShopEntity entity=new TqlPandianPeriodShopEntity();
        if(flag)
        {
            //搜索字段，只有是管理员的情况下才有效
            if(StringUtils.isNotEmpty(shopName)){
                entity.setDeptName(shopName);
            }
        }
        else {
            //String deptCode=commonService.findShopDeptCodeByUserId(userId);
            String deptCode = SecurityUtils.getLoginUser().getSysUser().getDeptCode();
            if (StringUtils.isNotEmpty(deptCode)){
                if (deptCode.length() > 12){
                    entity.setDeptCode(deptCode.substring(0,12));
                }else {
                    entity.setDeptCode(deptCode);
                }
            }
        }
        List<PandianVo> list=pandianPeriodService.queryList(entity);
        return success(list);
    }

    @ApiOperation("PC端门店盘点列表")
    @GetMapping("/pcList")
    public TableDataInfo pcList(PeriodPcListDTO dto)
    {

        List<Map>  list=pandianPeriodService.queryPcList1(dto);
        return getDataTable(list);
//        return success(ResultDTO.success(pandianPeriodService.queryPcList(dto),pandianPeriodService.queryPcListCount(dto)));
//        //return success();
    }

    @ApiOperation("选盘点时间段")
    @GetMapping("/getTimeSlot")
    public AjaxResult getTimeSlot(){
        Long userId = SecurityUtils.getLoginUser().getUserid();
        boolean flag= SecurityUtils.isAdmin(userId);
        TqlPandianPeriodShopEntity entity=new TqlPandianPeriodShopEntity();
        if(!flag){
            String deptCode = SecurityUtils.getLoginUser().getSysUser().getDeptCode();
            if (StringUtils.isNotEmpty(deptCode)){
                if (deptCode.length() > 12){
                    entity.setDeptCode(deptCode.substring(0,12));
                }else {
                    entity.setDeptCode(deptCode);
                }
            }
        }
        List<PandianVo> list=pandianPeriodService.getTimeSlot(entity);
        return success(list);
    }


    /**
     * 修改盘点
     * @param list
     * @return
     */
    @ApiOperation("盘点时间修改")
    @Log(title = "盘点时间修改", businessType = BusinessType.UPDATE)
    @PostMapping("/editPandian")
    public AjaxResult editPandian(@RequestBody List<TqlPandianPeriodEntity> list){
        return success(pandianPeriodService.editPandianPeriod(list));
    }


    /**
     * 新增盘点
     * @param list
     * @return
     */
    @ApiOperation("新增盘点日期")
    @Log(title = "新增盘点日期", businessType = BusinessType.INSERT)
    @PostMapping("/addPandian")
    public AjaxResult addPanDian(@RequestBody List<TqlPandianPeriodEntity> list){
        return success(pandianPeriodService.addPanDian(list));
    }

    /**
     * 盘点列表
     * @param pcode
     * @return
     */
    @ApiOperation("查看盘点日期列表")
    @Log(title = "查看盘点日期列表", businessType = BusinessType.OTHER)
    @PostMapping("/editMainPanDian/{pcode}")
    public AjaxResult editMainPanDian(@PathVariable String pcode){
        LoginUser loginUser= SecurityUtils.getLoginUser();
        Long userId=loginUser.getUserid();
        boolean flag= SecurityUtils.isAdmin(userId);
        PanDianPeriodDTO dto = new PanDianPeriodDTO();
        dto.setMonthString(pcode);
        if (!flag){
            String deptCode = SecurityUtils.getLoginUser().getSysUser().getDeptCode();
            if (deptCode.length() > 12){
                dto.setDeptCode(deptCode.substring(0,12));
            }else {
                dto.setDeptCode(deptCode);
            }
        }
        return success(pandianPeriodService.queryPanDianPeriod(dto));
    }

    /**
     * 盘点列表
     * @param pcode
     * @return
     */
    @ApiOperation("盘点日期管理-删除盘点")
    @Log(title = "盘点日期管理-删除盘点", businessType = BusinessType.UPDATE)
    @PostMapping("/delMainPanDian/{pcode}")
    public AjaxResult delMainPanDian(@PathVariable String pcode){
        return success(pandianPeriodService.delMainPanDian(pcode));
    }

    /**
     * 盘点管理-关联
     * @param pcode
     * @return
     */
    @ApiOperation("盘点日期管理-关联")
    @Log(title = "盘点日期管理-关联", businessType = BusinessType.UPDATE)
    @PostMapping("/editAssociationPanDian/{pcode}")
    public AjaxResult editAssociationPanDian(@PathVariable String pcode){
        return success(pandianPeriodService.editAssociationPanDian(pcode));
    }

    /**
     * 盘点管理-关联
     * @param id
     * @return
     */
    @ApiOperation("盘点日期管理-确认关联")
    @Log(title = "盘点日期管理-确认关联", businessType = BusinessType.UPDATE)
    @PostMapping("/editAssociationConfirmPanDian/{id}")
    public AjaxResult editAssociationConfirmPanDian(@PathVariable Long id){
        return success(pandianPeriodService.editAssociationConfirmPanDian(id));
    }




}
